package com.copyctrl.campus.portal.handler;


import com.copyctrl.campus.common.exception.JsonException;
import com.copyctrl.campus.common.model.ApiResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLException;

/**
 * <p>
 * 统一异常处理
 * </p>
 */
@ControllerAdvice
@Slf4j
public class MyExceptionHandler {
    private static final String DEFAULT_ERROR_VIEW = "error";

    /**
     * 统一 json 异常处理
     *
     * @param exception JsonException
     * @return 统一返回 json 格式
     */
    @ExceptionHandler(value = JsonException.class)
    @ResponseBody
    public ApiResponse jsonErrorHandler(JsonException exception) {
        log.error("【JsonException】:{}", exception.getMessage());
        return ApiResponse.ofException(exception);
    }

    /**
     * 统一SQL一异常处理
     * @param exception SQLException
     * @return
     */
    @ExceptionHandler(value = SQLException.class)
    @ResponseBody
    public ApiResponse SQLExceptionHandler(SQLException exception) {
        log.error("【SQLException】:{}", exception.getMessage());
        return ApiResponse.ofException();
    }


}
